#include "upper.h"
#include "usermanager.h"
#include "cJSON.h"
#include "opm.h"
#include "wss.h"
#include "oxc.h"
#include "user.h"
#include "update.h"

char* json_to_string(cJSON* json){
    char* p;
    if(json == NULL) return NULL;
    p = cJSON_Print(json);
    cJSON_Delete(json);
    return p;      
}

cJSON* json_insert_packetid(cJSON* json,char* packetid){
    if(json == NULL || packetid == NULL) return NULL;
    cJSON_ReplaceItemInObject(json,"packetid",cJSON_CreateString(packetid));
    return json;
}

cJSON* response_packet(int id,int return_code,cJSON* param,int time){
    char s_time[32] = {0};
    cJSON* root = cJSON_CreateObject();
    cJSON_AddItemToObject(root,"id",cJSON_CreateNumber(id));
    cJSON_AddItemToObject(root,"packetid",cJSON_CreateString(""));
    if(param){
        cJSON_AddItemToObject(root,"param",param);
    }    
    if(time > 0){
        time_to_string(time,s_time,"ms");
        if(param == NULL){
            param = cJSON_CreateObject();
            cJSON_AddItemToObject(root,"param",param);
        }
        cJSON_AddItemToObject(param,"processingTime",cJSON_CreateString(s_time)); 
    }
    
    cJSON_AddItemToObject(root,"returnCode",cJSON_CreateNumber(return_code));
    cJSON_AddItemToObject(root,"returenMessage",cJSON_CreateString(return_error_message(return_code))); 
    return root;       
}

char* request_packet(int id,cJSON* param){
    char* p;
    char s_time[32] = {0};
    cJSON* root = cJSON_CreateObject();
    cJSON_AddItemToObject(root,"id",cJSON_CreateNumber(id));
    cJSON_AddItemToObject(root,"packetid",cJSON_CreateString(""));
    if(param){
        cJSON_AddItemToObject(root,"param",param);
    }    
    p = cJSON_Print(root);
    cJSON_Delete(root);
    return p;     
}

cJSON* error_packet(int error_code){
    return response_packet(id_protocol_exception,error_code,NULL,0);
}

char* parse_upper_protocol(char* data,user_socket* p_user){
    cJSON* root = NULL;
    cJSON* item = NULL;
    cJSON* response_json = NULL;
    char* s_json = NULL;
    char* packetid;
    int id;
    root = cJSON_Parse(data);
    if(!root){                 
        return NULL;
    } 
    item = cJSON_GetObjectItem(root,"id");
    if(item == NULL) {
        cJSON_Delete(root);
        return NULL;
    } 
    id = item->valueint;
    item = cJSON_GetObjectItem(root,"packetid");
    if(item == NULL) {
        cJSON_Delete(root);
        return NULL;
    } 
    packetid = item->valuestring;  

    item = cJSON_GetObjectItem(root,"param");
    switch(id){
        case id_user_login:
            response_json = usermanager_parse(id,item,p_user);
            break;
#ifndef DEBUG
        if(p_user->username){
#endif        
            case id_user_loginout:
            case id_user_changepwd:
                response_json = usermanager_parse(id,item,p_user);
                break;
            break;        
            case id_alarm_report:
            case id_alarm_check:
                response_json = alarm_parse(id,item);
                break;
            case id_update:
                response_json = update_parse(item);
                break;
            case id_opm_scan:
            case id_opm_reset:
                response_json = opm_parse(id,item);
                break;
            case id_wss_get_custom:
            case id_wss_set_custom:
            case id_wss_reset:
            case id_wss_get_info:           	
                response_json = wss_parse(id,item);
                break;

            case id_oxc_set_custom:
            case id_oxc_get_custom:
            case id_oxc_reset:
            case id_oxc_get_info:
            case id_oxc_get_power:
            case id_oxc_set_atten:
            case id_oxc_get_atten:
                response_json = oxc_parse(id,item);
                break;
            case id_save_default:
            case id_load_default:
                response_json = part_default(id);            
                break;
            default:
                response_json = error_packet(UNKNOWN_ID);
                break;
#ifndef DEBUG                
        }
#endif        
    }    
    json_insert_packetid(response_json,packetid);
    s_json = json_to_string(response_json);
    cJSON_Delete(root);
    return s_json;
}